package com.hy.mydesktop.client.component.mvc.factory;

import java.util.List;

import com.extjs.gxt.ui.client.mvc.Controller;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.mvc.controller.meta.WindowControllerModelEnum;
import com.hy.mydesktop.client.mvc.grid.GridController;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentControllerMetaNodeModel;

/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-9-19；时间：上午11:16:17</li>
 * <li>类型名称：ContentPanelControllerFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class GridControllerFactory {
	/**
	 * registerEventTypes(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT); 1 注册事件 2
	 * handleEvent
	 * 
	 */

	public static Controller createGridController(
			GxtComponentControllerMetaNodeModel gxtComponentMetaModel) {

		return configureGrid(gxtComponentMetaModel);
	}

	/**
	 * 
	 * <ul>
	 * <li>方法含义：对窗口的属性进行配置</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-9-19；时间：下午上午11:16:37</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * 
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static Controller configureGrid(
			GxtComponentControllerMetaNodeModel gxtComponentMetaNodeModel) {

		GridController gridController = createDefaultGridController();

		// 注册GridController，需要监听的事件
		if (gxtComponentMetaNodeModel.get(WindowControllerModelEnum.VIEWDATA
				.name().toLowerCase()) != null) {

			String viewDataCode = gxtComponentMetaNodeModel
					.get(WindowControllerModelEnum.VIEWDATA.name()
							.toLowerCase());
			gridController.setViewData(viewDataCode);

		}
		// 为Controller添加它的标识 id
		if (gxtComponentMetaNodeModel.get(WindowControllerModelEnum.ID.name()
				.toLowerCase()) != null) {

			String id = gxtComponentMetaNodeModel
					.get(WindowControllerModelEnum.ID.name().toLowerCase());
			gridController.setId(id);

		}
		// 注册GridController，需要监听的事件
		if (gxtComponentMetaNodeModel
				.get(WindowControllerModelEnum.REGISTEREVENTTYPES.name()
						.toLowerCase()) != null) {

			List<String> appEventCodes = gxtComponentMetaNodeModel
					.get(WindowControllerModelEnum.REGISTEREVENTTYPES.name()
							.toLowerCase());
			for (String code : appEventCodes) {
				Integer i = Integer.valueOf(code);
				switch (i) {
				case 1:// Integer.valueOf("0001")
					gridController.addRegisterEventType(AppEvents.DESKTOP_USER_LOGIN);

					break;
				case 2:// Integer.valueOf("0002")

					break;
				case 6:// Integer.valueOf("0006")
					gridController
							.addRegisterEventType(AppEvents.DESKTOP_WINDOW_INIT);
					break;
				case 7:// Integer.valueOf("0007")
					gridController
							.addRegisterEventType(AppEvents.DESKTOP_WINDOWAPPLICATION_INIT);
					break;
				case 8:// Integer.valueOf("0008")
					gridController
							.addRegisterEventType(AppEvents.DESKTOP_APPLICATION_RENDER);
					break;
				case 101:// Integer.valueOf("0101")
					gridController.addRegisterEventType(AppEvents.WINDOW_ININT);
					break;
				case 201:// Integer.valueOf("0201")
					gridController.addRegisterEventType(AppEvents.CONTENTPANEL_ININT);
					break;
				case 401:// Integer.valueOf("0201")
					gridController.addRegisterEventType(AppEvents.LOAD_DATA_EVENT);
					break;

				default:
					break;
				}
			}
		}

		// 配置WindowController，需要监听的每类事件，对应的handleEvent方法
		/*
		 * if(gxtComponentMetaModel.get(WindowControllerModelEnum.HANDLEEVENT.name
		 * ().toLowerCase())!=null){
		 * 
		 * List list =
		 * gxtComponentMetaModel.get(WindowControllerModelEnum.HANDLEEVENT
		 * .name().toLowerCase());
		 * 
		 * switch (componentTypeEnum) { case FITLAYOUT_MODEL:
		 * layout=FitLayoutFactory.createFitLayout(gxtComponentMetaModel);
		 * window.setLayout(layout); break; case ACCORDIONLAYOUT_MODEL:
		 * layout=AccordionLayoutFactory
		 * .createAccordionLayout(gxtComponentMetaModel);
		 * window.setLayout(layout); break;
		 * 
		 * default: break; }
		 * 
		 * }
		 * if(gxtComponentMetaModel.get(WindowModelEnum.MINWIDTH.name().toLowerCase
		 * ())!=null){ //设置窗口的最小宽度
		 * window.setMinWidth((Integer)gxtComponentMetaModel
		 * .get(WindowModelEnum.MINWIDTH.name().toLowerCase()));
		 * 
		 * }
		 * 
		 * if(gxtComponentMetaModel.get(WindowModelEnum.LAYOUT.name().toLowerCase
		 * ())!=null){ //设置布局方式 ComponentTypeEnum componentTypeEnum =
		 * (ComponentTypeEnum
		 * )gxtComponentMetaModel.get(WindowModelEnum.LAYOUT.name
		 * ().toLowerCase());
		 * 
		 * Layout layout=null;
		 * 
		 * switch (componentTypeEnum) { case FITLAYOUT_MODEL:
		 * layout=FitLayoutFactory.createFitLayout(gxtComponentMetaModel);
		 * window.setLayout(layout); break; case ACCORDIONLAYOUT_MODEL:
		 * layout=AccordionLayoutFactory
		 * .createAccordionLayout(gxtComponentMetaModel);
		 * window.setLayout(layout); break; case ROWLAYOUT_MODEL:
		 * layout=RowLayoutFactory.createRowLayout(gxtComponentMetaModel);
		 * window.setLayout(layout); break; case VBOXLAYOUT_MODEL:
		 * layout=VboxLayoutFactory.createVboxLayout(gxtComponentMetaModel);
		 * window.setLayout(layout); break; case COLUMNLAYOUT_MODEL:
		 * layout=ColumnLayoutFactory.createColumnLayout(gxtComponentMetaModel);
		 * window.setLayout(layout); break; case FLOWLAYOUT_MODEL:
		 * layout=FlowLayoutFactory.createFlowLayout(gxtComponentMetaModel);
		 * window.setLayout(layout); break; case TABLELAYOUT_MODEL:
		 * layout=TableLayoutFactory.createTableLayout(gxtComponentMetaModel);
		 * window.setLayout(layout); break; case FORMLAYOUT_MODEL:
		 * layout=FormLayoutFactory.createFormLayout(gxtComponentMetaModel);
		 * 
		 * window.setLayout(layout);
		 * 
		 * break; default: break; }
		 * 
		 * } if(gxtComponentMetaModel.get(WindowModelEnum.TOPCOMPONENT.name().
		 * toLowerCase())!=null){ //设置窗口的顶部组建
		 * window.setTopComponent((Component)gxtComponentMetaModel
		 * .get(WindowModelEnum.TOPCOMPONENT.name().toLowerCase()));
		 * 
		 * }
		 */

		return gridController;
	}

	private static GridController createDefaultGridController() {
		GridController gridController = new GridController();

		return gridController;
	}
}
